clear all
set more off
cap log close

do "H:\Lavecchia_7086\to-transfer-jan-2022\RESTAT_Replication_Programs\0_Set_Directories.do"

****************************************************************************
* This do file calculates exemption left 
* 
****************************************************************************

cap log using "$dir_log\2e_Calculate_Exemption_Left.log", replace

****************************************************************************
***** A:  Calculate left over exemption amount **********
****************************************************************************


use "$dir_data\capital_income_final_8299.dta"



keep if year>=1984  & year<=1994
keep lin__i year clkgli ggex_i 


* calculate  used exemption amount in 

gen temp= ggex_/0.5 if year>=1984 & year<=1987
replace temp= ggex_/0.667 if year>=1988 & year<=1989
replace temp= ggex_/0.75 if year>=1990 & year<=1994

* identify people who report too large of an exemption
gen flag=.
replace flag=1 if temp>100000
replace flag=1 if temp>50000 & (year==1986 | year==1987)
replace flag=1 if temp>25000 & (year==1985)
*
replace temp=0 if flag==1
* calculate amount of exemption used
sort lin__i year
by lin__i: gen cum_ggex1=sum(temp)
replace temp=clkgli if year==1985
by lin__i: gen cum_ggex2=sum(temp)
replace year=year+1
gen left_exemption1=0 
replace left_exemption1=20000 if year==1985
replace left_exemption1=50000-cum_ggex1 if year==1986 | year==1987
replace left_exemption1=100000-cum_ggex1 if year>=1988 & year<=1994
gen left_exemption2=0 
replace left_exemption2=20000-cum_ggex2 if year==1985
replace left_exemption2=50000-cum_ggex2 if year==1986 | year==1987
replace left_exemption2=100000-cum_ggex2 if year>=1988 & year<=1994


keep lin__i year left_exemption1 left_exemption2 flag
sort lin__i year

save "$dir_data\exemption_left.dta", replace


log close
